<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2023/9/6
  Time: 15:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
    <title>BookList</title>
    <link rel="stylesheet" href="/css/bookList.css">
</head>

<body>
<h1>
    <%--  判断用户是否登录  --%>
    <% if (request.getSession().getAttribute("user") != null) { %>
    欢迎用户，${user.user_name} 使用图书管理系统
    <div class="box1">
        <a href="${pageContext.request.contextPath }/toUser">
            <button>我的信息</button>
        </a>
        <a href="${pageContext.request.contextPath }/logout">
            <button>退出登录</button>
        </a>
    </div>
    <% } else if (request.getSession().getAttribute("admin") != null) {%>
    欢迎管理员，${admin.name} 使用图书管理系统
    <%}%>
</h1>
<div id="select">
    <img src="https://img1.qidian.com/upload/gamesy/2022/02/14/20220214161349tbwcaz84rz.jpg" alt="">
<form action="${pageContext.request.contextPath}/bookListLike" method="post">
    <input type="text" name="book_name" value="">
    <input type="submit" value="搜索">
</form>
</div>
<table border="1" cellspacing="0" align="center">
    <thead>
    <tr>
        <td>书名</td>
        <td>作者</td>
        <td>出版时间</td>
        <td>简介</td>
        <td>图书类型</td>
        <td>借阅用户</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${books}" var="book">
        <tr>
            <td>《${book.book_name}》</td>
            <td>${book.book_author}</td>
            <td><fmt:formatDate value='${book.book_time}' pattern='yyyy-MM-dd'/></td>
            <td value="${book.book_content}">${book.book_content}</td>
            <td>${book.book_type}</td>
            <c:if test="${'未借阅'.equals(book.user_name) }">
                <td style="background-color:greenyellow">${book.user_name}</td>
            </c:if>
            <c:if test="${!'未借阅'.equals(book.user_name) }">
                <td style="background-color:pink">${book.user_name}</td>
            </c:if>
            <td>
                    <%--  判断用户是否登录  --%>
                <% if (request.getSession().getAttribute("user") != null) { %>
                <c:if test="${book.user_id == 0}">
                    <button style="background-color: green"><a href="/myBook?user_id=${user.id}&book_id=${book.id}">借阅</a></button>
                </c:if>
                <c:if test="${book.user_id == user.id}">
                    <button><a href="/noMyBook?book_id=${book.id}">归还</a></button>
                </c:if>
                <% } else if (request.getSession().getAttribute("admin") != null) {%>
                <button>
                    <a href="/deleteBook?id=${book.id}">删除</a>
                </button>
                <button>
                    <a href="/toUpdateBook?id=${book.id}">修改</a>
                </button>
                <%}%>
            </td>

        </tr>
    </c:forEach>
    </tbody>
</table>
<div id="pagefy">
    <form action="${pageContext.request.contextPath }/bookList" id="bookForm">
        <input type="hidden" name="page" id="page" value="${page}">
        <input type="hidden" name="pageNumber" id="pageNumber" value="${pageNumber}">
        <a href="javascript:void(0)" onclick="submitMessageForm('first')">
            <button>首页</button>
        </a>
        <a href="javascript:void(0)" onclick="submitMessageForm('pre')">
            <button>上一页</button>
        </a>
        <span style="color: aliceblue">${page} / ${pageNumber}</span>
        <a href="javascript:void(0)" onclick="submitMessageForm('next')">
            <button>下一页</button>
        </a>
        <a href="javascript:void(0)" onclick="submitMessageForm('last')">
            <button>尾页</button>
        </a>
    </form>
</div>
<div class="btn">
    <%--  判断用户是否登录  --%>
    <% if (request.getSession().getAttribute("admin") != null) { %>
    <a href="/toAddBook">
        <button>添加图书</button>
    </a>
    <% } %>

</div>
<img src="https://gameapp-1252317822.file.myqcloud.com/yuewen/game/images/c8432fc0097150ffce8e27c19e3d41f8.jpg" alt="">
</body>
<script>
    function submitMessageForm(flag) {
        //获取当前页
        let c = Number(document.getElementById("page").value);
        if ("first" == flag) { //首页
            document.getElementById("page").value = 1;
        } else if ("pre" == flag) { //上一页
            if (c > 1) {
                document.getElementById("page").value = c - 1;
            }
        } else if ("next" == flag) {
            //总页数
            let l = Number(document.getElementById("pageNumber").value);
            if (c < l) {
                document.getElementById("page").value = c + 1;
            }
        } else if ("last" == flag) {
            document.getElementById("page").value = document.getElementById("pageNumber").value;
        }
        document.getElementById("bookForm").submit();
    }

    // var td=document.querySelector("tbody td:nth-child(4)");
    // var content=document.querySelector("tbody td:nth-child(4)").innerHTML;
    // td.addEventListener('click',function (){
    //     alert(content)
    // })
    var td=document.querySelectorAll("tbody td:nth-child(4)");
    for ( var i=0;i<td.length;i++){
        console.log(td[i])
        td[i].addEventListener('click',function (e){
            var content=e.target.innerHTML
            alert(content)
        })
    }



</script>
</html>
